﻿using System;
using System.Threading.Tasks;

namespace Ecobee.Net.Demo
{
	public class RefreshTokenCommand : CommandBase
	{
		public RefreshTokenCommand()
			: base("2", "Refresh Token")
		{
		}

		protected async override Task<bool> OnExcute(Api api)
		{
			bool returnValue = false;

			if (api.Access.IsValid)
			{
				// ***
				// *** Refresh the token so it can be used with API calls. The
				// *** refresh is done by the ecobee API.
				// ***
				if (await api.Access.Refresh())
				{
					Console.WriteLine("Token was refresh successfully.");
					returnValue = true;
				}
				else
				{
					Console.WriteLine("Token refresh failed.");
				}
			}
			else
			{
				Console.WriteLine("The token is not valid; Request Authorization first.");
			}

			return returnValue;
		}
	}
}
